package com.boruiimage.demo.controller;

import com.boruiimage.demo.doMain.Result;
import com.boruiimage.demo.repository.registerDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginUser {
    //登录
@Autowired
   private registerDao rd;
  @RequestMapping("/loginUser")
  @ResponseBody
  public Result selectUser(@RequestParam(value = "userName")String userName,
                           @RequestParam(value = "userPassword")String userPassword,HttpServletRequest request) {
          Result rs;
          rs=rd.selectUSer(userName,userPassword);
          System.out.println(rs);
           if(rs.getMsg().equals("登录成功")){
               selectUserS(userName,request);
           }
           return rs;
  }

    @RequestMapping("/loginUserS")
    public String selectUserS(String userName,
                             HttpServletRequest request) {
        request.getSession().setMaxInactiveInterval(86400);//设置一天的session时间
        request.getSession().setAttribute("userName",userName);
        System.out.println(userName);
        return "redirect:/index";
    }
    @RequestMapping("/index")
    public String show(){
        System.out.println("sss");
        return "index";
    }
    //用户退出
    @RequestMapping("/exit")
    @ResponseBody
    public Result exit(@RequestParam(value = "userName")String userName,HttpServletRequest request){
        Result rs=new Result();
        if(userName!=null){
            request.getSession().invalidate();
            rs.setMsg("成功");
        }else {
            rs.setMsg("失败");
        }
        System.out.println(userName);
        return rs;
    }

}
